package com.medishare.adapter;

import java.util.ArrayList;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.medishare.R;
import com.medishare.activity.ActivityCallCenter;
import com.medishare.data.DataMediReminder;
import com.medishare.util.Utils;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class AdapterMediRemiander extends BaseAdapter {
	private Context mContext;
	private ArrayList<DataMediReminder> mData;
	private LayoutInflater mInflater;
    public AdapterMediRemiander(Context context,ArrayList<DataMediReminder> datas){
    	this.mContext = context;
    	this.mData = datas;
    	mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public DataMediReminder getItem(int position) {
		mData.get(position);
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		DataMediReminder data = mData.get(position);
		if(convertView == null){
			convertView = mInflater.inflate(R.layout.listview_item_medi_reminder, null);
			holder = new ViewHolder();
			holder.createTime = (TextView) convertView.findViewById(R.id.create_time);
			holder.namePerson = (TextView) convertView.findViewById(R.id.name_person_list_item_medi_reminder);
			holder.nameDrug = (TextView) convertView.findViewById(R.id.name_drug_list_item_medi_reminder);
			holder.infoPrescribe = (TextView) convertView.findViewById(R.id.info_prescribe_list_item_medi_reminder);
			holder.usageMedicine = (TextView) convertView.findViewById(R.id.usage_medicine_list_item_medi_reminder);
			holder.remark = (TextView) convertView.findViewById(R.id.remark_list_item_medi_reminder);
			holder.medicineConsult = (Button) convertView.findViewById(R.id.medicine_consult);
			holder.buyMedicine = (Button) convertView.findViewById(R.id.buy_medicine);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		holder.createTime.setText(Utils.convertTime(data.getUpdate_time()));
		holder.namePerson.setText(data.getName());
		holder.nameDrug.setText(data.getMdc_name());
		holder.infoPrescribe.setText(data.getMdc_rx());
		holder.usageMedicine.setText(data.getMdc_usage());
		holder.remark.setText(data.getRemark());
		onClick(holder.buyMedicine,data);
		onClick(holder.medicineConsult,data);
		return convertView;
	}
    
	
	static class ViewHolder{
		public TextView createTime;
		public TextView namePerson;
		public TextView nameDrug;
		public TextView infoPrescribe;
		public TextView usageMedicine;
		public TextView remark;
		public Button medicineConsult;
		public Button buyMedicine;
	}
	
	
	public void onClick(Button button,final DataMediReminder data){
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String url = "";
				AsyncHttpClient client = new AsyncHttpClient();
				switch (v.getId()) {
				case R.id.medicine_consult:
				    url = Utils.getBaseUrl() + "Call/insert/?uuid=" + Utils.getDeviceUUID(mContext) + "&member_id=" + data.getMember_id() + "&type=2";  
					client.get(url, new AsyncHttpResponseHandler(){

						@Override
						public void onSuccess(String content) {
							super.onSuccess(content);
							
						}
						
					});
					break;

				case R.id.buy_medicine://��ҩ��ѯ
				    url = Utils.getBaseUrl() + "Call/insert/?uuid=" + Utils.getDeviceUUID(mContext) + "&member_id=" + data.getMember_id() + "&type=1";  
					client.get(url, new AsyncHttpResponseHandler(){

						@Override
						public void onSuccess(String content) {
							super.onSuccess(content);
							
						}
						
					});
					break;
					
				default:
					break;
				}
				Intent i = new Intent(mContext,ActivityCallCenter.class);
				mContext.startActivity(i);
			}
		});
	}
	
}
